import 'package:amway_superapp_creator_plugin/mock/mock_data_generator.dart';
import 'package:amway_superapp_creator_plugin/models/guanfang_image.dart';
import 'package:amway_superapp_creator_plugin/models/user_icon_item.dart';
import 'package:amway_superapp_creator_plugin/utils/toast_util.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/base_model.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_flavor/flutter_flavor.dart';
import 'package:uuid/uuid.dart';

import '../api.dart';

class UserIconVM extends BaseModel {
  bool _showGridShadow = false;
  bool get showGridShadow => _showGridShadow;
  set showGridShadow(bool value) {
    _showGridShadow = value;
    notifyListeners();
  }

  final List<UserIconItem> items = List();

  int _currentItem = 0;

  UserIconVM({@required Api api}) {
    this.api = api;
    processTestData();
  }

  void processTestData() {
    List<String> urlList = MockData.getUrlList();
    for (int i = 0; i < urlList.length; i++) {
      items.add(new UserIconItem(urlList.elementAt(i), i));
    }
    // items.elementAt(_currentItem).isSelected = true;
  }

  void setItemSelected(int index) {
    items.elementAt(_currentItem).isSelected = false;
    items.elementAt(index).isSelected = true;
    _currentItem = index;
    notifyListeners();
  }

  String getMainUrl() {
    if(items!=null&&items.isNotEmpty){
      return items.elementAt(_currentItem).picUrl;
    }
    return '';
  }

  void markCached(String cachedIcon) {
    for(int i=0;i<items.length;i++){
      if(items[i].picUrl==cachedIcon){
        items[i].isSelected=true;
        _currentItem=i;
        notifyListeners();
        break;
      }
    }
  }

  void fetchIconList() async{
    items.clear();
    api.getUserIconList((data) {
          List<GuanfangImage>guanfangImages=[];
          data.forEach((element) {
            GuanfangImage image=GuanfangImage(url: element);
            guanfangImages.add(image);
          });
          if(guanfangImages.isNotEmpty){
            for(int i=0;i<guanfangImages.length;i++){
              items.add(UserIconItem(guanfangImages[i].url, i));
            }
            notifyListeners();
          }
    }, (data) {
      ToastUtils.showLongToast("图片列表拉取失败");
    });
  }
  //   var queryParameters = {
  //     "q": "官方头像",
  //     "start": 0,
  //     "rows": 100,
  //     "sort": "-publishdate"
  //   };
  //   var uuid = Uuid();
  //   // Generate a v1 (time-based) id
  //   var v1 = uuid.v1();
  //   var heards = {
  //     "Authorization": FlavorConfig.instance.variables["AppCode"],
  //     "X-Ca-Nonce": v1,
  //   };
  //   var amwayBaseUrl = FlavorConfig.instance.variables["amwayBaseUrl"];
  //   Options options = Options(headers: heards);
  //   Dio dio = Dio();
  //   Response response = await dio.get(amwayBaseUrl + "/images",
  //       options: options,queryParameters: queryParameters);
  //   if (response.statusCode == 200) {
  //     List<GuanfangImage>guanfangImages=[];
  //     response.data.forEach((element) {
  //       guanfangImages.add(GuanfangImage.fromJson(element));
  //     });
  //     if(guanfangImages.isNotEmpty){
  //       items.clear();
  //       for(int i=0;i<guanfangImages.length;i++){
  //         items.add(UserIconItem(guanfangImages[i].url, i));
  //       }
  //       notifyListeners();
  //     }
  //   }else{
  //     print(response.statusCode);
  //   }
  // }
}
